package com.kgc.sbt.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;

/**
 * Created on 2021/7/27.
 * <p>
 * Author : Lcywings
 * <p>
 * Description :
 */
@Data
public class PageSupportUtil<T> implements Serializable {

    /**
     * 序列号
     */
    private static final long serialVersionUID = -4231713626794111022L;
    /**
     * 总页数
     */
    @ApiModelProperty("总页数")
    private int totalPage;

    /**
     * 总条数
     */
    @ApiModelProperty("总条数")
    private int totalCount;

    /**
     * 起始行
     */
    @ApiModelProperty("起始行")
    private int pageIndex;

    /**
     * 当前页码
     */
    @ApiModelProperty("当前页码")
    private int currPageNo;

    /**
     * 分页显示条数
     */
    @ApiModelProperty("分页显示条数")
    private int pageSize;

    /**
     * 分页数据
     */
    @ApiModelProperty("分页数据")
    private Collection<T> data;

    /**
     * 当设置总条数时，可以确定总页数
     */
    public void setTotalCount(int totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
            this.totalPage = this.totalCount % this.pageSize == 0 ?
                    this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1;
        }
    }

    /**
     * 当设置当前页码时，可以确定起始行
     */
    public void setCurrPageNo(int currPageNo) {
        if (currPageNo > 0) {
            this.currPageNo = currPageNo;
            this.pageIndex = (this.currPageNo - 1) * this.pageSize;
        }
    }
}
